﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class TraCuuNhanVien : XtraUserControl
    {
        private int MaChucVu;

        public TraCuuNhanVien()
        {
            InitializeComponent();
        }

        public TraCuuNhanVien(int MaChucVu)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.MaChucVu = MaChucVu;
        }

        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            TimKiemNhanVien();
        }

        private void TimKiemNhanVien()
        {
            var tx = tboxTimKiem.MaskBox;
            AutoCompleteStringCollection customSource = new AutoCompleteStringCollection();
            List<NhanVienDTO> list = new List<NhanVienDTO>();
            list = NhanVienDAO.LoadDanhSachNhanVien();
            string[] arr = new string[list.Count];
            int i = 0;
            foreach (NhanVienDTO p in list)
            {
                arr[i] = (p.TenNhanVien);
                i++;
            }
            customSource.AddRange(arr);
            string tenNhanVien = tboxTimKiem.Text;
            DataTable tb = NhanVienDAO.TimKiemDaiLyCoBan(tenNhanVien);
            tx.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tx.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tx.AutoCompleteCustomSource = customSource;
            gridNhanVienControl.DataSource = tb;
        }

        private void repositoryItemPicturePhanQuyen_Click(object sender, EventArgs e)
        {
            string MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhanQuyen frm = new PhanQuyen(MaNhanVien);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemPictureDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa nhân viên này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                NhanVienDTO nv = new NhanVienDTO();
                nv.MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
                if (NhanVienDAO.deleteNhanVien(nv))
                {
                    // MessageBox.Show("Xoá thành công");
                    TimKiemNhanVien();
                }
                else
                {
                    MessageBox.Show("Xoá thành thất bại");
                }
            }
        }

        private void repositoryItemPictureCapNhat_Click(object sender, EventArgs e)
        {
            string MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
            string TenNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["TenNhanVien"]).ToString();
            string DienThoai = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["DienThoai"]).ToString();
            string DiaChi = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["DiaChi"]).ToString();
            string Email = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["Email"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            CapNhatNhanVien frm = new CapNhatNhanVien(MaNhanVien, TenNhanVien, DiaChi, Email, DienThoai);
            tb.Controls.Add(frm, 0, 1);
        }

        private void gridViewDanhSachNhanVien_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {

            if (e.IsGetData)
            {
                e.Value = QLDaiLy.Properties.Resources.deletexoagia;
            }

            if (e.Column.Caption == "Cập Nhật")
            {
                e.Value = QLDaiLy.Properties.Resources.add_user;
            }
            if (e.Column.Caption == "Phân Quyền")
            {
                e.Value = QLDaiLy.Properties.Resources.updateUser;
            }
            
        }
    }
}
